Skip to main content

Fragment

Integration of MobaiBiometric fragment with complete UI

This example shows how to integrate the Biometric Capture Session to your project. The Biometric Capture Session extracts a frames collection from the camera preview.

This Example app uses ViewBinding, so enable it with the following (at the end of the android{} block):

buildFeatures {
viewBinding true
}

When prompted, click Sync Now, and we will be ready to use the Biometric Capture Session in our app.

Create an activity to host MBCaptureSessionFragment.

OverlayActivity.kt

class OverlayActivity : AppCompatActivity(){

}

fragment_capture_overlay.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OverlayActivity">

<!-- res/layout/example_activity.xml -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

Add a fragment container.

   <androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

OverlayActivity.kt

class OverlayActivity : AppCompatActivity()) {
private lateinit var binding: ActivityOverlayBinding
}

Create an ActivityOverlayBinding instance.

private lateinit var binding: ActivityOverlayBinding

Invoke onCreate method.

   @SuppressLint("UnsafeOptInUsageError")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityOverlayBinding.inflate(layoutInflater)
setContentView(binding.root)

verifyCameraPermission()
}

Initialize binding.

 binding = ActivityOverlayBinding.inflate(layoutInflater)
setContentView(binding.root)

Request camera permission.OverlayActivity is the activity host forCaptureOverlayFragment.

verifyCameraPermission()

Extend OverlayActivityt from MBCaptureSessionFragmentListener.

class OverlayActivity : AppCompatActivity(), MBCaptureSessionFragmentListener {
private lateinit var binding: ActivityOverlayBinding

@SuppressLint("UnsafeOptInUsageError")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityOverlayBinding.inflate(layoutInflater)
setContentView(binding.root)
verifyCameraPermission()
}

override fun onCaptureFinished(result: MBCaptureSessionResult?) { }

override fun onFailure(errorEnum: MBCaptureSessionError) { }
}

Display camera by inflating MBCaptureSessionFragment.

class OverlayActivity : AppCompatActivity(), MBCaptureSessionFragmentListener {
private lateinit var binding: ActivityOverlayBinding

@SuppressLint("UnsafeOptInUsageError")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityOverlayBinding.inflate(layoutInflater)
setContentView(binding.root)
verifyCameraPermission()


val options = MBCaptureSessionOptions.Builder()
.build()

val captureSessionFragment = MBCaptureSessionFragment(options = options,CaptureSessionFragmentListener = this)

if (savedInstanceState == null) {
supportFragmentManager.commit {
add(R.id.fragment_container_view, captureSessionFragment)
setReorderingAllowed(true)
}
}
}

override fun onCaptureFinished(result: MBCaptureSessionResult?) { }

override fun onFailure(errorEnum: MBCaptureSessionError) { }
}

Build an MBCaptureSessionOption.Builder instance.. All arguments are optional. It contains the default options for performing the capture session. Those default options can be redefined. In this case the capture process is set to automatic.

 val options = MBCaptureSessionOptions.Builder().build()

MBCaptureSessionOption.Builder arguments are optional. Options description:

  • numberOfFrameToCollect Describes the number of frames to collect during the capture session
  • frameInterval After collecting the first frame, is the number of frames to skip before collecting a frame
  • numberOfFramesBeforeCapture Describes the number of frames to skip before it starts collecting
  • automaticCapture Tells whether the capture is automatic or manual
  • targetResolution Are the resolution values from the caller device
  • timeBeforeAutomaticCapture Is the set time in seconds the capture should wait to start collecting frames
  • cameraSelector Describes whether we want to use a front or rear camera

Create an MBCaptureSessionFragment instance.

 val captureSessionFragment = MBCaptureSessionFragment(options = options,CaptureSessionFragmentListener = this)

Add MBCaptureSessionFragment to the activity.

  if (savedInstanceState == null) {
supportFragmentManager.commit {
add(R.id.fragment_container_view, captureSessionFragment)
setReorderingAllowed(true)
}
}

onCaptureFinished is executed for MBCaptureSessionService when the capture session has successfully finished.

 override fun onCaptureFinished(result: MBCaptureSessionResult?) { }

MBCaptureSessionResult arguments description:

  • faceImage A high quality image captured after collecting frames (not implemented)
  • captureSessionData Is all the collected data in protobuf format (not implemented)
  • frames Are the frame collection.

Function onFailure is executed when the capture process fails. MBCaptureSessionError tells the type of error that occurred.

  override fun onFailure(errorEnum: MBCaptureSessionError) { }

MBCaptureSessionError entries:

  • UNABLE_TO_OPEN_CAMERA Describes failure while opening the camera
  • UNABLE_TO_COLLECT_FRAMES The capture has is aborted